package com.zhuangjie.coupon.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.coupon.dto.ShopCouponBatchDTO;
import com.zhuangjie.coupon.service.CouponBatchService;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.security.annotations.RequiresRoles;
import com.zhuangjie.shop.enums.ShopRole;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2024-02-28
 */
@RestController
@RequestMapping("/coupon-batch")
public class CouponBatchController {
    @Autowired
    private CouponBatchService couponBatchService;

    @ApiOperation("编辑或创建店铺优惠券")
    @PutMapping("/putShopCouponBatch")
    @RequiresRoles(modules = ModuleSecurityNames.SHOP,value = ShopRole.admin)
    public R putShopCouponBatch(@Valid @RequestBody ShopCouponBatchDTO shopCouponBatchDTO) {
        return R.capture(()-> couponBatchService.putShopCouponBatch(shopCouponBatchDTO));
    }

    @ApiOperation("查询店铺优惠券批次列表")
    @GetMapping("/shop-coupon-batch")
    @RequiresRoles(modules = ModuleSecurityNames.SHOP,value = ShopRole.admin)
    public R queryShopCouponBatchList() {
        return R.require(List.class, couponBatchService.queryShopCouponBatchList());
    }

    @ApiOperation("删除店铺优惠券")
    @DeleteMapping("/deleteShopCouponBatch/{couponBatchId}")
    @RequiresRoles(modules = ModuleSecurityNames.SHOP,value = ShopRole.admin)
    public R deleteShopCouponBatch(@PathVariable("couponBatchId") Long couponBatchId) {
        return R.capture(()-> couponBatchService.removeShopCouponBatchById(couponBatchId));
    }

    @ApiOperation("查询店铺优惠券批次信息")
    @GetMapping("/shop-coupon-batch/{couponBatchId}")
    @RequiresRoles(modules = ModuleSecurityNames.SHOP,value = ShopRole.admin)
    public R queryShopCouponBatch(@PathVariable("couponBatchId") Long couponBatchId) {
        return R.require(ShopCouponBatchDTO.class, couponBatchService.getShopCouponBatchById(couponBatchId));
    }

    @ApiOperation("用户领取指定店铺内优惠券")
    @PostMapping("/receive-shop-public-coupon/{batchId}")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R receiveShopPublicCoupon(@NotEmpty(message = "batchId不能为空") @PathVariable("batchId") Long batchId) throws Exception {
        return R.capture(()->couponBatchService.receiveShopPublicCoupon(batchId));
    }

}
